/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.oa.entity.TestAudit;
import com.thinkgem.jeesite.modules.stock.entity.Warehouse;
import com.thinkgem.jeesite.modules.stock.entity.Woods;
import com.thinkgem.jeesite.modules.stock.service.WarehouseService;
import com.thinkgem.jeesite.modules.stock.service.WoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.entity.InOrder;
import com.thinkgem.jeesite.modules.stock.service.InOrderService;

import java.util.List;

/**
 * 入库Controller
 * @author 入库
 * @version 2019-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/inOrder")
public class InOrderController extends BaseController {

	@Autowired
	private InOrderService inOrderService;
	
	@ModelAttribute
	public InOrder get(@RequestParam(required=false) String id) {
		InOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = inOrderService.get(id);
		}
		if (entity == null){
			entity = new InOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:inOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(InOrder inOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InOrder> page = inOrderService.findPage(new Page<InOrder>(request, response), inOrder); 
		model.addAttribute("page", page);
		return "modules/stock/inOrderList";
	}

	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private WoodsService woodsService;
	@RequiresPermissions("stock:inOrder:view")
	@RequestMapping(value = "view")
	public String view(InOrder inOrder, Model model) {

		model.addAttribute("inOrder", inOrder);
		return "modules/stock/inOrderView";
	}
	@RequiresPermissions("stock:inOrder:view")
	@RequestMapping(value = "form")
	public String form(InOrder inOrder, Model model) {
		List<Woods> woods= woodsService.findList(new Woods());
		model.addAttribute("woods",woods);
		if (org.apache.commons.lang3.StringUtils.isNotBlank(inOrder.getId())){
			// 环节编号
			String taskDefKey = inOrder.getAct().getTaskDefKey();
			model.addAttribute("inOrder", inOrder);
			if(taskDefKey.equals("audit1")){
				return "modules/stock/inOrderaudit1Form";
			}
			else if(taskDefKey.equals("audit2")){
				List warehouses= warehouseService.findList(new Warehouse());
				model.addAttribute("warehouses",warehouses);
				return "modules/stock/inOrderaudit2Form";
			}
			else if(taskDefKey.equals("audit3")){

				return "modules/stock/inOrderaudit3Form";
			}
			else if(taskDefKey.equals("audit4")){

				return "modules/stock/inOrderaudit4Form";
			}
            else{
				return "modules/stock/inOrderaudit5Form";
			}

		}



		return "modules/stock/inOrderForm";
	}
	/**
	 * 工单执行（完成任务）
	 * @param model
	 * @return
	 */
	@RequiresPermissions("stock:inOrder:edit")
	@RequestMapping(value = "saveAudit")
	public String saveAudit(InOrder inOrder, Model model) {
		if (org.apache.commons.lang3.StringUtils.isBlank(inOrder.getAct().getFlag())
				|| org.apache.commons.lang3.StringUtils.isBlank(inOrder.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(inOrder, model);
		}
		inOrderService.auditSave(inOrder);
		return "redirect:" + adminPath + "/act/task/todo/";
	}

	@RequiresPermissions("stock:inOrder:edit")
	@RequestMapping(value = "save")
	public String save(InOrder inOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, inOrder)){
			return form(inOrder, model);
		}
		inOrderService.save(inOrder);
		addMessage(redirectAttributes, "保存入库成功");
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	@RequiresPermissions("stock:inOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(InOrder inOrder, RedirectAttributes redirectAttributes) {
		inOrderService.delete(inOrder);
		addMessage(redirectAttributes, "删除入库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/inOrder/?repage";
	}

}